// pages/user/myInvoice/myInvoice.js
var app = getApp();
var _PAGESIZE = 20;//默认页量
var _PAGE = 1;//默认页码
var load = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],//列表

    hasMore: false, //数据是否加载完成
    loading: false,
    load_show: false,//整体加一个开关 加载
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    _PAGE = 1;
    that.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      loading: true
    })
    _PAGE = 1;
    that.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    that.setData({
      loading: true
    })
    _PAGE++;
    that.getList();
  },

  //获取我的订单列表
  getList() {
    var that = this;
    app.ajax({
      url: "User/Invoice/getRecord",
      method: "POST",
      load: load,
      msg: '加载中...',
      data: { page: _PAGE, pageSize: _PAGESIZE },
      success: function (res) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              list: res.data.data,
              empty: false
            });
          } else {
            that.setData({
              list: that.data.list.concat(res.data.data),
              empty: false
            });
          }
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load = false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          load_show: true
        })

      }
    })
  },

  //保存发票图片
  saveImgBtn(e){
    var that = this;
    var imgSrc = e.currentTarget.dataset.url;
    wx.downloadFile({
      url: imgSrc,
      success: function (res) {
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            app.showToast('保存成功');
          },
        })
      }
    })
    //获取相册授权
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.showModal({
            content: '你已拒绝授权无法保存图片,如需重新授权请前往(我的-设置-设置权限)',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                
              } else if (res.cancel) {
                
              }
            }
          })
        }
      }
    })
  },
  // 查看大图
  previewImage(e) {
    var currentImg = e.currentTarget.dataset.url;
    wx.previewImage({
      current: currentImg, // 当前显示图片的http链接
      urls: [currentImg] // 需要预览的图片http链接列表
    })
  },
})